package com.xmkj.carwash.activity;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.CameraUpdateFactory;
import com.amap.api.maps2d.LocationSource;
import com.amap.api.maps2d.MapView;
import com.amap.api.maps2d.UiSettings;
import com.amap.api.maps2d.model.BitmapDescriptorFactory;
import com.amap.api.maps2d.model.CameraPosition;
import com.amap.api.maps2d.model.LatLng;
import com.amap.api.maps2d.model.Marker;
import com.amap.api.maps2d.model.MarkerOptions;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.geocoder.GeocodeResult;
import com.amap.api.services.geocoder.GeocodeSearch;
import com.amap.api.services.geocoder.RegeocodeAddress;
import com.amap.api.services.geocoder.RegeocodeQuery;
import com.amap.api.services.geocoder.RegeocodeResult;
import com.common.location.LocationUtils;
import com.common.mvp.BaseMvpActivity;
import com.common.mvp.BasePresenter;
import com.xmkj.carwash.R;
import com.xmkj.carwash.bean.TestData;
import com.xmkj.carwash.utils.SensorEventHelper;
import com.xmkj.carwash.utils.StatusBarUtil;
import com.xmkj.carwash.utils.ViewToBitmap;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/4/8.
 */

public class MapDetailActivity extends BaseMvpActivity implements AMap.OnMarkerClickListener, LocationSource, AMap.OnCameraChangeListener, GeocodeSearch.OnGeocodeSearchListener {
    @BindView(R.id.map)
    MapView mapView;
    @BindView(R.id.iv)
    ImageView iv;
    @BindView(R.id.tv_address)
    TextView tvAddress;
    private GeocodeSearch geocodeSearch;

    @Override
    protected BasePresenter createPresenterInstance() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_map_info;
    }

    @Override
    protected void onViewCreated() {
        Bundle saveInstanceState = getSaveInstanceState();
        mapView.onCreate(saveInstanceState);
    }

    @Override
    protected void doLogicFunc() {
        initMap();
        //地理搜索类
        geocodeSearch = new GeocodeSearch(this);
        geocodeSearch.setOnGeocodeSearchListener(this);
        mLocationOption = new AMapLocationClientOption();
        //设置定位监听
        //设置为高精度定位模式
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        //设置定位参数
        mlocationClient.setLocationOption(mLocationOption);
        LatLng latlng = new LatLng(30.299977, 120.313794);
        getAddressByLatlng(latlng);
        //j加点
        pullDataOnNet("30.299977","120.313794");
        //移动
        LocationUtils.moveCamera(new LatLng(30.299977, 30.299977), aMap);
    }
    private void pullDataOnNet(String latitude, String longitude) {

        TestData testData = new TestData();
        testData.lat = Double.valueOf(latitude);
        testData.lng = Double.valueOf(longitude);
        LatLng latlng = new LatLng(testData.lat, testData.lng);
        addMarkersToMap(latlng);


/*        TestData testData1 = new TestData();
        testData.lat = 30.31472206;
        testData.lng = 120.33699799;
        LatLng latlng1 = new LatLng(testData.lat, testData.lng);
        addMarkersToMap(latlng1);*/


    }
    //网地图上添加mark
    private void addMarkersToMap(LatLng latlng) {

        ImageView imageView = new ImageView(this);
        imageView.setImageResource(R.mipmap.wash_dot);
        Bitmap bitmap = ViewToBitmap.convertViewToBitmap(imageView);
        drawMarkerOnMap(latlng, bitmap);
    }
    //画图
    private Marker drawMarkerOnMap(LatLng point, Bitmap markerIcon) {

        if (aMap != null && point != null) {

            Marker marker = aMap.addMarker(new MarkerOptions().anchor(0.5f, 1)
                    .position(point)
//                    .title(id)
                    .icon(BitmapDescriptorFactory.fromBitmap(markerIcon)));
            return marker;

        }

        return null;


    }
    private AMap aMap;
    private SensorEventHelper mSensorHelper;
    private void initMap() {
        if (aMap == null) {
            aMap = mapView.getMap();
            aMap.setOnMarkerClickListener(this);
            setUpMap();
        }
        mSensorHelper = new SensorEventHelper(this);
        if (mSensorHelper != null) {
            mSensorHelper.registerSensorListener();
        }
    }
    private UiSettings uiSettings;
    private void setUpMap() {
        aMap.setLocationSource(this);// 设置定位监听
        // 设置默认定位按钮是否显示
        uiSettings = aMap.getUiSettings();
        //显示默认的定位按钮
        uiSettings.setMyLocationButtonEnabled(false);
        uiSettings.setZoomControlsEnabled(false);//隐藏放大 缩小 按钮
        // 可触发定位并显示当前位置
        aMap.setMyLocationEnabled(true);// 设置为true表示显示定位层并可触发定位，false表示隐藏定位层并不可触发定位，默认是false
        aMap.moveCamera(CameraUpdateFactory.zoomTo(18));
        aMap.setOnCameraChangeListener(this);
    }
    @Override
    public void bindKnife() {
        ButterKnife.bind(this);
   }

    @Override
    public boolean onMarkerClick(Marker marker) {
        return false;
    }
    private OnLocationChangedListener mListener;
    private AMapLocationClient mlocationClient;
    private AMapLocationClientOption mLocationOption;
    @Override
    public void activate(OnLocationChangedListener onLocationChangedListener) {
        mListener = onLocationChangedListener;
        if (mlocationClient == null) {
            mlocationClient = new AMapLocationClient(this);

            // 此方法为每隔固定时间会发起一次定位请求，为了减少电量消耗或网络流量消耗，
            // 注意设置合适的定位时间的间隔（最小间隔支持为2000ms），并且在合适时间调用stopLocation()方法来取消定位请求
            // 在定位结束后，在合适的生命周期调用onDestroy()方法
            // 在单次定位情况下，定位无论成功与否，都无需调用stopLocation()方法移除请求，定位sdk内部会移除


        }
    }

    @Override
    public void deactivate() {
    }

    @Override
    public void onCameraChange(CameraPosition cameraPosition) {

    }

    @Override
    public void onCameraChangeFinish(CameraPosition cameraPosition) {

    }
    @Override
    protected void setStatusBar() {
        StatusBarUtil.setStatusBarColor(this, R.color.black);
    }
    @Override
    protected void setNavigation() {
        super.setNavigation();
        m_statusBar.setVisibility(View.GONE);
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onResume() {
        super.onResume();
        mapView.onResume();
        if (mSensorHelper != null) {
            mSensorHelper.registerSensorListener();
        }
    }
    /**
     * 方法必须重写
     */
    @Override
    protected void onPause() {
        super.onPause();
        if (mSensorHelper != null) {
            mSensorHelper.unRegisterSensorListener();
            mSensorHelper.setCurrentMarker(null);
            mSensorHelper = null;
        }
        mapView.onPause();
        deactivate();
    }
    /**
     * 方法必须重写
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mapView.onSaveInstanceState(outState);
    }
    /**
     * 方法必须重写
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
        if (null != mlocationClient) {
            mlocationClient.onDestroy();
        }
    }

    @Override
    public void onRegeocodeSearched(RegeocodeResult regeocodeResult, int i) {
        RegeocodeAddress regeocodeAddress = regeocodeResult.getRegeocodeAddress();
        String formatAddress = regeocodeAddress.getFormatAddress();
       String simpleAddress = formatAddress.substring(9);
        tvAddress.setText( simpleAddress);
    }

    @Override
    public void onGeocodeSearched(GeocodeResult geocodeResult, int i) {

    }
    private void getAddressByLatlng(LatLng latLng) {
        //逆地理编码查询条件：逆地理编码查询的地理坐标点、查询范围、坐标类型。
        LatLonPoint latLonPoint = new LatLonPoint(latLng.latitude, latLng.longitude);
        RegeocodeQuery query = new RegeocodeQuery(latLonPoint, 500f, GeocodeSearch.AMAP);
        //异步查询
        geocodeSearch.getFromLocationAsyn(query);
    }
}
